package com.example.minesweepers;

import high_scores.HighScores;
import high_scores.Player;
import interfaces.IPlayer;

import com.google.gson.Gson;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Display;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HighScoresActivity extends Activity {
	private String gameType;
	private HighScores highScores;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_high_scores);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		
		gameType = getIntent().getStringExtra("GameType");
		
		highScores = new HighScores();
		loadHighScores(gameType);
		
		
		laodScoresToTheScreen();
		
	}

	private void laodScoresToTheScreen() {
		LinearLayout highScoresLayout = (LinearLayout) findViewById(R.id.highScores);
		for(int i = 0; i < highScores.size(); i++) {
			TextView top15 = new TextView(this);
			top15.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/minesweeper_font.ttf"));
			top15.setTextColor(Color.WHITE);
			top15.setTextSize(TypedValue.COMPLEX_UNIT_PX, getScreenHeight()/15);
			top15.setText((i + 1) + ". " + highScores.getPlayer(i).getName() + " : " + highScores.getPlayer(i).getTime());
			highScoresLayout.addView(top15);
		}
	}
	
	private void loadHighScores(String gameType) {
		SharedPreferences settings = getSharedPreferences(gameType, MODE_PRIVATE);
		Gson gson = new Gson();
		
		int sizeToLoad = settings.getInt("Size", 0);
		for(int i = 0; i < sizeToLoad; i++) {
			String json = settings.getString("Player" + i, "");
			IPlayer player = gson.fromJson(json, Player.class);
	    	highScores.putUnsorted(player);
		}
	}
	
	
	private int getScreenHeight() {
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		return size.y;
	}
}
